<template>
    <div id="list">
        <el-card shadow="never" body-style="padding: 24px;">
            <el-table
            :data="courseTables"
            :height="tableHeight"
            :span-method="arraySpanMethod"
            :cell-style='cellStyleMethod'
            @cell-click='cellCLickMethod'
            border
            tooltip-effect="light"
        >
                <el-table-column label="课程编号" align="left"  width="90" fixed>
                    <template slot-scope="props">
                        <div v-if="props.row.isCategoryRow">
                            <el-row>
                                <el-col :span="2">
                                    <div
                                        class="float-left"
                                        style="display:inline-block;height:50px;line-height:50px;"
                                    >
                                        <i v-if="props.row.directionCode" style="margin-left:20px;">
                                            &nbsp;
                                        </i>
                                        <span>
                                            {{ props.row.sequence }}{{ props.row.categoryName }}
                                        </span>
                                    </div>
                                </el-col>
                                <!-- <el-col :span="18" :push="1">
                                    <div
                                        class="float-left"
                                        style="margin-left:40px;height:50px;line-height: 50px"
                                        v-if="!isRead"
                                    >
                                        <el-button
                                            size="mini"
                                            @click="add(props.$index)"
                                            v-if="props.row.showAddCourseBtn"
                                        >
                                            新增课程
                                        </el-button>
                                        <el-button
                                            size="mini"
                                            @click="copyBefore(props.row)"
                                            v-if="
                                                dataType == '2' &&
                                                    props.row.showAddCourseBtn &&
                                                    !props.row.isDirection
                                            "
                                        >
                                            复制之前
                                        </el-button>

                                        <template
                                            v-if="props.row.hasCourse && props.row.showAddCourseBtn"
                                        >
                                            <el-button
                                                size="mini"
                                                @click="showDivideModule(props.$index, 1)"
                                            >
                                                分模块
                                            </el-button>
                                            <el-button
                                                size="mini"
                                                @click="showDivideModule(props.$index, 2)"
                                            >
                                                分组
                                            </el-button>
                                        </template>
                                        <template
                                            v-if="
                                                dataType == 2 &&
                                                    props.row.showAddCourseBtn &&
                                                    !props.row.isDirection
                                            "
                                        >
                                            <el-button
                                                size="mini"
                                                @click="selectDirection(props.$index)"
                                                v-if="
                                                    rowData.directionList &&
                                                        rowData.directionList.length > 0 &&
                                                        !props.row.directionCode &&
                                                        props.row.hasCourse
                                                "
                                            >
                                                分方向
                                            </el-button>
                                        </template>
                                    </div>
                                </el-col> -->
                            </el-row>
                        </div>
                        <!-- <div v-else>
                            <el-checkbox
                                v-model="courseTables[props.$index].isCourseCheck"
                                :key="props.row.id"
                                v-if="
                                    !isRead &&
                                        props.row.isCourseRow &&
                                        !props.row.courseGroupId &&
                                        !props.row.parentId
                                "
                                @change="pickCourse(props.row.id, props.$index, 1)"
                            ></el-checkbox>
                            <span>{{ props.row.code }}</span>
                        </div> -->
                        <div v-else>
                            <el-checkbox
                                v-model="courseTables[props.$index].isCourseCheck"
                                :key="props.row.id"
                                v-if="
                                    !isRead &&
                                    props.row.isCourseRow &&
                                    !props.row.courseGroupId &&
                                    !props.row.parentId &&
                                    props.row.exception
                                "
                                @change="pickCourse(props.row.id, props.$index, 1)"
                            ></el-checkbox>
                            <span>{{ props.row.code }}</span>
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column width="200" align="center" label="课程名称" fixed  show-overflow-tooltip>
                    <template slot-scope="props">
                        <a
                            @click="showCourseDetail(props.row)"
                            v-if="dataType == '2'"
                            v-bind:class="{
                                'new-record': props.row.changeStatus == 1,
                                'edit-record': props.row.changeStatus == 2,
                                'remove-record': props.row.changeStatus == 3
                            }"
                        >
                            {{ props.row.name }}
                        </a>
                    </template>
                </el-table-column> -->
                <el-table-column width="200" align="center" label="课程名称" fixed  show-overflow-tooltip>
                    <template slot-scope="props">
                        <a
                            @click="showCourseDetail(props.row)"
                            v-if="dataType == '2'"
                            v-bind:class="{
                                'new-record': props.row.changeStatus == 1,
                                'edit-record': props.row.changeStatus == 2,
                                'remove-record': props.row.changeStatus == 3
                            }"
                        >
                            {{ props.row.name }}
                        </a>
                        <a
                            @click="showCourseDetail(props.row)"
                            v-if="dataType == '1'"
                            v-bind:class="{
                                'new-record': props.row.changeStatus == 1,
                                'edit-record': props.row.changeStatus == 2,
                                'remove-record': props.row.changeStatus == 3
                            }"
                        >
                            {{ props.row.name }}
                        </a>
                    </template>
                </el-table-column>
                <el-table-column
                    min-width="150"
                    align="center"
                    label="模块"
                    style="position:relative;"
                >
                    <template slot-scope="props">
                        <div v-if="props.row.isCategoryRow && props.row.isPublicElective != 1">
                            <el-row>
                                <el-col :span="18" :push="1">
                                    <div
                                        class="float-left"
                                        style="margin-left:40px;height:50px;line-height: 50px"
                                        v-if="!isRead"
                                    >
                                        <el-button
                                            size="mini"
                                            @click="add(props.$index)"
                                            v-if="props.row.showAddCourseBtn"
                                        >
                                            新增课程
                                        </el-button>
                                        <el-button
                                            size="mini"
                                            @click="copyBefore(props.row)"
                                            v-if="
                                                dataType == '2' &&
                                                    props.row.showAddCourseBtn &&
                                                    !props.row.isDirection
                                            "
                                        >
                                            复制之前
                                        </el-button>

                                        <template
                                            v-if="props.row.hasCourse && props.row.showAddCourseBtn"
                                        >
                                            <el-button
                                                size="mini"
                                                @click="showDivideModule(props.$index, 1)"
                                            >
                                                分模块
                                            </el-button>
                                            <el-button
                                                size="mini"
                                                @click="showDivideModule(props.$index, 2)"
                                            >
                                                分组
                                            </el-button>
                                            <el-button
                                                size="mini"
                                                v-if="dataType == 2"
                                                @click="bathDeleta(props.$index)"
                                            >
                                                批量删除
                                            </el-button>
                                            <el-button
                                                size="mini"
                                                @click="bathEdit(props.$index)"
                                            >
                                                批量编辑
                                            </el-button>
                                        </template>
                                        <template
                                            v-if="
                                                dataType == 2 &&
                                                    props.row.showAddCourseBtn &&
                                                    !props.row.isDirection
                                            "
                                        >
                                            <el-button
                                                size="mini"
                                                @click="selectDirection(props.$index)"
                                                v-if="
                                                    rowData.directionList &&
                                                        rowData.directionList.length > 0 &&
                                                        !props.row.directionCode &&
                                                        props.row.hasCourse
                                                "
                                            >
                                                分方向
                                            </el-button>
                                        </template>
                                    </div>
                                </el-col>
                            </el-row>
                        </div>
                        <div v-if="props.row.courseGroupId">
                            <el-checkbox
                                v-if="!isRead && !props.row.parentId"
                                v-model="courseTables[props.$index].isModuleCheck"
                                @change="pickCourse(props.row.id, props.$index, 2)"
                            ></el-checkbox>
                            <span>{{ props.row.modelName }}</span>
                            <i
                                class="el-icon-delete"
                                @click="deleteGroup(props.row.courseGroupId)"
                                v-if="!isRead && !props.row.parentId"
                                style="color:red;cursor:pointer;position:absolute;right:4px;bottom:8px;padding:4px"
                                title="删除模块"
                            ></i>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="分组"
                    align="center"
                    show-overflow-tooltip
                    style="position:relative;"
                >
                    <template slot-scope="props" v-if="props.row.parentId">
                        <el-checkbox
                            v-if="!isRead"
                            v-model="courseTables[props.$index].isGroupCheck"
                            @change="pickCourse(props.row.id, props.$index, 3)"
                        ></el-checkbox>
                        <span>{{ props.row.groupName }}</span>
                        <i
                            class="el-icon-delete"
                            @click="deleteGroup(props.row.parentId)"
                            v-if="!isRead"
                            style="color:red;cursor:pointer;position:absolute;right:4px;bottom:8px;padding:4px"
                            title="删除课程分组"
                        ></i>
                    </template>
                </el-table-column>
                <el-table-column label="考试/查" align="center" width="70">
                    <template slot-scope="props">
                        <el-select
                            v-if="props.row.edit && props.row.status != 2"
                            v-model="currentRow.examMode"
                            size="mini"
                        >
                            <el-option
                                v-for="item in options.types"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                        <span v-else>{{ props.row.examModeName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="学分" align="center" width="54">
                    <template slot-scope="props">
                        {{ props.row.studyScore }}
                    </template>
                </el-table-column>
                <el-table-column label="学时/周数" align="center" width="85">
                    <template slot-scope="props">
                        {{ +props.row.weeksType == 1 ? `${props.row.studyScore}周` : props.row.period}}
                    </template>
                </el-table-column>
                <el-table-column label="是否必修" align="center" width="80">
                    <template slot-scope="props">
                        <!-- <template v-if="props.row.edit && props.row.status != 2"> -->
                        <template v-if="props.row.edit && props.row.status != 2">
                            <el-select v-model="currentRow.compulsory">
                                <el-option key="1" label="是" value="1"></el-option>
                                <el-option key="0" label="否" value="0"></el-option>
                            </el-select>
                        </template>
                        <template v-else slot-scope="props">
                            {{ !props.row.compulsory ? '' : props.row.compulsory == '1' ? '是' : '否' }}
                        </template>
                    </template>
                </el-table-column>
                <el-table-column label="上机时间" align="center" width="80">
                    <template slot-scope="props">
                        <el-input
                            v-if="props.row.edit"
                            v-model="currentRow.computeTime"
                            size="mini"
                            @change="fixNum1"
                            type="number"
                            onkeyup="this.value=this.value.replace(/[^\d.]/g,'');"
                            :min="0"
                        />
                        <span v-else>{{ props.row.computeTime }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="实验时数" align="center" width="80">
                    <template slot-scope="props">
                        <el-input
                            v-if="props.row.edit"
                            v-model="currentRow.experimentTime"
                            size="mini"
                            type="number"
                            onkeyup="this.value=this.value.replace(/[^\d.]/g,'');"
                            :min="0"
                            @change="fixNum2"
                        />
                        <span v-else>{{ props.row.experimentTime }}</span>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="各学期周学时分配/周数分配" align="center">
                    <el-table-column
                        :label="val"
                        align="center"
                        width="90"
                        v-for="(val, idx) in semesterSorts"
                        :key="'semester' + (idx + 1)"
                    >
                        <template slot-scope="props">
                            <template v-if="props.row.edit">
                                <template v-if="currentRow.weeksType * 1 == 0">
                                    <el-select v-model="currentRow['semester' + (idx + 1)]" clearable>
                                        <el-option
                                            v-for="(item, key) in semesterCodes"
                                            :key="key"
                                            :label="item"
                                            :value="key"
                                        ></el-option>
                                    </el-select>
                                </template>
                                <template v-else>
                                    <el-select v-model="currentRow['semester' + (idx + 1)]" clearable>
                                        <el-option
                                            v-for="(item, key) in semesterCodes"
                                            :key="key"
                                            :label="item"
                                            :value="key"
                                        ></el-option>
                                    </el-select>
                                </template>
                            </template>
                            <template v-else>
                                <span v-if="props.row.weeksType == 0">
                                    {{ props.row['semester' + (idx + 1)] }}
                                </span>
                                <span v-else>
                                    {{ semesterCodes[props.row['semester' + (idx + 1)]] }}
                                </span>
                            </template>
                        </template>
                    </el-table-column>
                </el-table-column> -->
                <el-table-column label="各学期周学时分配/周数分配" align="center">
                    <el-table-column align="center"  :label="schooolTermItem.label" v-for='(schooolTermItem, k) in semesterListMap' :key='k'>
                        <template slot-scope="props">
                            <div v-if='!props.row.edit && props.row.semesters && props.row.semesters.indexOf(k + 1 + "") > -1'>
                                <span v-if="props.row.weeksType == 0">
                                    {{ props.row.weekHour }}
                                </span>
                                <span v-else>
                                    暑期
                                </span>
                            </div>
                            <div v-if='props.row.edit && props.row.semesterListMap[k].checked'>
                                <span v-if="props.row.weeksType == 0">
                                    {{ props.row.weekHour }}
                                </span>
                                <span v-else>
                                    暑期
                                </span>
                            </div>
                        </template>
                    </el-table-column>
                    <!-- <el-table-column
                        :label="val.label"
                        align="center"
                        width="90"
                        v-for="(val, idx) in semesterSorts"
                        :key="'semester' + (idx + 1)"
                    >
                        <template slot-scope="props">
                            <template v-if="props.row.edit">
                                <template v-if="currentRow.weeksType * 1 == 0">
                                    <el-select v-model="currentRow['semester' + (idx + 1)]" @change="$set(currentRow, 'semester' + (idx + 1), $event)" clearable>
                                        <el-option
                                            :key="val.value"
                                            :label="props.row.weekHour"
                                            :value="val.value"
                                        ></el-option>
                                    </el-select>
                                </template>
                                <template v-else>
                                    <el-select v-model="currentRow['semester' + (idx + 1)]" @change="checkSemester(val,currentRow['semester' + (idx + 1)])" clearable>
                                        <el-option
                                            v-for="(item, key) in semesterCodes"
                                            :key="key"
                                            :label="item"
                                            :value="key"
                                        ></el-option>
                                    </el-select>
                                </template>
                            </template>
                            <template v-else>
                                <span v-if="props.row.weeksType == 0">
                                    <span v-if="props.row.semesters.indexOf(idx + 1) != -1">
                                        {{ props.row.weekHour }}
                                    </span>
                                </span>
                                <span v-else>
                                    <span v-if="props.row.semesters.indexOf(idx + 1) != -1">
                                        {{ semesterCodes[0] }}
                                    </span>
                                </span>
                            </template>
                        </template>
                    </el-table-column> -->
                </el-table-column>
                <el-table-column label="备注" min-width="150" align="center">
                    <template slot-scope="props">
                        <el-input
                            v-if="props.row.edit"
                            v-model="currentRow.remarks"
                            size="mini"
                            maxlength="250"
                            placeholder="请输入备注"
                        />
                        <span :title="props.row.remarks" v-else>
                            {{ props.row.remarks }}
                        </span>
                    </template>
                </el-table-column>
                <el-table-column v-if="!isRead" label="操作" width="120" fixed="right" align="center">
                    <template slot-scope="props">
                        <template v-if="props.row.isCourseRow">
                            <template v-if="props.row.edit">
                                <el-button type="text" @click="saveCourse(props.row)">
                                    保存
                                </el-button>
                                <el-button type="text" @click="cancelEdit(props.$index)">
                                    取消
                                </el-button>
                            </template>
                            <!-- <template v-else>
                                <el-button type="text" @click="edit(props.$index)">编辑</el-button>
                                <el-button type="text" @click="remove(props.$index)">删除</el-button>
                            </template> -->
                            <template v-else>
                                <el-button type="text" @click="edit(props.$index)">编辑</el-button>
                                <el-button type="text" @click="remove(props.$index)" :disabled="dataType == 1 && props.row.changeStatus == 3">删除</el-button>
                            </template>
                            <span></span>
                        </template>
                    </template>
                </el-table-column>
            </el-table>
            <el-row v-if="next" class="pd-12px float-right" style="padding-right: 0;">
                <el-col :span="24">
                    <el-button size="medium" @click="next(-1)">上一步</el-button>
                    <el-button size="medium" type="primary" @click="next()">下一步</el-button>
                </el-col>
            </el-row>
        </el-card>
        <el-dialog
            :title="divideModuleForm.title"
            width="70%"
            :visible.sync="showDivideModuleDialog"
            style="padding:20px;"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <el-form
                :model="divideModuleForm"
                label-width="90px"
                label-position="right"
                ref="divideModuleForm"
                style="margin-top:12px;"
                :rules="rule"
                inline
            >
                <el-row>
                    <el-col span="8">
                        <el-form-item
                            :label="divideModuleForm.operate == 2 ? '课程组名：' : '模块名称：'"
                            prop="remark"
                            style="margin-left:12px"
                        >
                            <el-input
                                v-model="divideModuleForm.remark"
                                placeholder="请输入"
                                style="width:100%;"
                                size="mini"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col span="7" v-if="divideModuleForm.operate == 2">
                        <el-radio
                            v-model="limitType"
                            style="margin-top:8px;"
                            label="0"
                            @change="radioChoose"
                            :disabled="!canChangeLimitType"
                        >
                            <span></span>
                        </el-radio>
                        <el-form-item label="至少门数：" prop="minNum" style="margin-right:0">
                            <el-input-number
                                v-model="divideModuleForm.minNum"
                                @change="autoName()"
                                onkeypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))"
                                :min="0"
                                :max="divideModuleForm.maxNum"
                                size="mini"
                                :disabled="limitType != '0'"
                                controls-position="right"
                                style="width:80px;"
                            ></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col span="9" v-if="divideModuleForm.operate == 2">
                        <el-radio
                            v-model="limitType"
                            style="margin-top:8px;"
                            label="1"
                            @change="radioChoose"
                            :disabled="!canChangeLimitType"
                        >
                            <span></span>
                        </el-radio>
                        <el-form-item label="学分限制：" prop="expression">
                            <el-select
                                v-model="divideModuleForm.expression"
                                style="width:90px"
                                @change="autoName()"
                                :disabled="limitType != '1'"
                            >
                                <el-option
                                    v-for="item in options.expressions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="minCredits">
                            <el-input-number
                                v-model="divideModuleForm.minCredits"
                                size="mini"
                                :min="0"
                                @change="autoName()"
                                controls-position="right"
                                style="width:80px;height:28px"
                                :disabled="limitType != '1'"
                            ></el-input-number>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <el-row :gutter="4">
                <el-col class="secondary-text">
                    <i class="el-icon-info" style="margin-top:4px;"></i>
                    <span>
                        {{
                            `已选择${divideModuleForm.num}${
                                divideModuleForm.level == 1 ? '门课程' : '个模块'
                            }`
                        }}
                    </span>
                </el-col>
            </el-row>
            <el-table
                stripe
                width="100%"
                :data="currentCourses"
                style="margin-top:12px;"
                :span-method="spanMethodForModule"
            >
                <el-table-column
                    label="模块"
                    align="center"
                    prop="modelName"
                    v-if="divideModuleForm.level !== 1"
                />
                <el-table-column label="课程代码" align="center" prop="code" />
                <el-table-column label="课程名称" align="center" prop="name" />
                <el-table-column label="学分" align="center" prop="studyScore" />
                <el-table-column label="总学时" align="center" prop="period" />
                <!-- <el-table-column label="开课学院" align="center" prop="college"/> -->
                <el-table-column label="备注" align="center" prop="remarks" />
            </el-table>
            <span slot="footer">
                <div style="margin:12px">
                    <el-button @click="showDivideModuleDialog = false">
                        {{ $t('message.cancel') }}
                    </el-button>
                    <el-button type="primary" @click="writeDivideRelation()">
                        {{ $t('message.confirm') }}
                    </el-button>
                </div>
            </span>
        </el-dialog>
        <courses
            ref="courseDialog"
            :random-id="randomId"
            :schoolTerm= "semesterListMap"
            :show="showCourseDialog"
            :is-elective="isElectiveForCurRow"
        />
        <batch-set-course 
            v-if="batchSetCourse.show" 
            :show.sync="batchSetCourse.show" 
            @confirm="batchSetCourseConfirm"
            :schoolTerm= "semesterListMap"
        />
        <el-dialog
            title="设置方向"
            width="42%"
            :visible.sync="showDirectionDialog"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
        >
            <el-table
                :data="rowData.directionList"
                ref="directionTable"
                stripe
                @selection-change="directionSelectionChange"
                style="width: 100%"
            >
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="方向代码" align="center" prop="directionCode" />
                <el-table-column label="方向名称" align="center" prop="directionName" />
            </el-table>
            <span slot="footer">
                <div style="margin:12px">
                    <el-button @click="showDirectionDialog = false">
                        {{ $t('message.cancel') }}
                    </el-button>
                    <el-button type="primary" @click="writeDirectionRel()">
                        {{ $t('message.confirm') }}
                    </el-button>
                </div>
            </span>
        </el-dialog>
        <!-- 复制之前 -->
        <el-dialog
            :visible.sync="copyVisible"
            title="复制之前"
            width="45%"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            @close="resetCopyForm"
        >
            <el-form
                ref="copyForm"
                :model="copyForm"
                label-width="80px"
                label-position="right"
                :rules="copyFormRules"
            >
                <el-row :gutter="24">
                    <el-col :span="12">
                        <el-form-item label="年级:" prop="year">
                            <el-date-picker
                                v-model="copyForm.year"
                                value-format="yyyy"
                                type="year"
                                placeholder="选择学年"
                                style="width: 100%"
                                @change="handleInitScheme"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item
                            :label="$t('culture.scheme.trainingLevel') + ':'"
                            prop="trainingLevel"
                        >
                            <el-select
                                v-model="copyForm.trainingLevel"
                                style="width: 100%"
                                clearable
                                @change="handleInitScheme"
                            >
                                <el-option
                                    v-for="item in copyOptions.trainingLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="'类别:'" prop="professionCateGory">
                            <el-select
                                v-model="copyForm.professionCateGory"
                                style="width: 100%;"
                                clearable
                                @change="handleInitScheme"
                            >
                                <el-option
                                    v-for="item in copyOptions.professionCateGory"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="培养方案:" prop="schemeId">
                            <el-select
                                v-model="copyForm.schemeId"
                                style="width: 100%"
                                @change="handleChangScheme"
                            >
                                <el-option
                                    v-for="item in copyOptions.schemes"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="课程类别:" prop="courseCategory">
                            <el-select v-model="copyForm.courseCategory" style="width: 100%">
                                <el-option
                                    v-for="item in copyOptions.courseCategorys"
                                    :key="item.labelId"
                                    :label="item.label"
                                    :value="item.labelId"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer">
                <div>
                    <el-button @click="copyVisible = false">
                        {{ $t('message.cancel') }}
                    </el-button>
                    <el-button type="primary" @click="handleSubmitCopy()">
                        {{ $t('message.confirm') }}
                    </el-button>
                </div>
            </span>
        </el-dialog>
        <courseDetail
            :couseDetail="couseDetail"
            :showCourseDialog="showCourseDetailDialog"
            @closeCourseDialog="handlerCloseCourseDialog"
            v-if="showCourseDetailDialog"
        ></courseDetail>
        
    </div>
</template>
<script>
import {
    courseLabelRelation,
    findLabelInfoByCultureId,
    addCourse,
    EditCourse,
    deleteCourse,
    getCourseList,
    addCourseLabelRelGroup,
    findDirectionDataByScheme,
    addDirectionLableRelToCourse,
    delDirectionLableRelToCourse,
    deleteCourseLabelRelGroup,
    findCultureSchemeListByYear,
    copyBackCourseLabelRelation,
    copyLabelCourseRelation,
    checkRepeatedMultiDirection,
    getSchemeSchoolTerm,
    getCultureTemplateById,
    batchDelCourseLabeRalation,
    batchUpdateCourseLabelRelation
} from 'common/src/api/culture/bachelor'
import { findTrainingLevelByProjId } from 'common/src/api/dictionary'
import { queryDic } from 'common/src/api/dictionary'
import Bus from 'common/src/utils/bus'
import { findExceptions } from 'common/src/api/culture/exceptionSetting'

import courses from './courses'
import courseDetail from './courseDetail'
export default {
    props: {
        next: {
            type: Function,
            default: null //下一步方法
        },
        templateId: {
            default: null //当前模板Id
        },
        templateIdOrigin: {
            //此id是原始模板id
            type: Number,
            default: null
        },
        isRead: {
            default: false //操作列是否可见
        },
        projId: {
            type: String,
            default: '1'
        },
        cultureSchemeForcourse: {
            type: Object,
            default: {}
        },
        dataType: {
            default: '1' //1为模板，2为方案
        },
        inChange: {
            type: Boolean,
            default: false
        },
        schoolTerms: {
            type: Number,
            default: 0 //学期
        },
        showSCteachArrangements: {
            //强化班教学安排 设置高度使用，其他地方不会用到
            type: Boolean,
            default: false
        },
        directionSelection: []
    },
    name: 'courseForm',
    components: {
        courses: courses,
        courseDetail,
        batchSetCourse: resolve => require(['./batchSetCourse'], resolve)
    },
    data() {
        const validate = (rule, value, callback) => {
            let flag = value === ''
            flag =
                flag &&
                ((this.limitType == '0' && (rule.field == 'minNum' || rule.field == 'maxNum')) ||
                    (this.limitType == '1' &&
                        (rule.field == 'expression' || rule.field == 'minCredits')))
            if (!flag) callback()
            else callback(new Error('请输入数字'))
        }
        const validateChange = (rule, value, callback) => {
            let flag = this.limitType == '1' && value === ''
            if (!flag) callback()
            else callback(new Error('请选择'))
        }
        const validateMinCredits = (rule, value, callback) => {
            let flag = this.limitType == '1' && isNaN(value)
            if (!flag) callback()
            else callback(new Error('请输入数字'))
        }
        const verifyName = (rule, value, callback) => {
            if (value.trim() != '') callback()
            else
                callback(
                    new Error(`请输入${this.divideModuleForm.operate == 1 ? '模块' : '课程组'}名称`)
                )
        }
        return {
            showDirectionDialog: false,
            dialogTitle: '',
            showDivideModuleDialog: false,
            currentCourses: [], //模态框列表数据
            divideModuleForm: {
                operate: '', //1:分模块，2：分组
                courseIdList: [], //勾选课程id集合
                remark: '', //模块名称
                level: '', //保存的当前勾选的复选框类型 1：课程；2：模块；3：分组
                type: '0', //分组时勾选的选项 0：至少门数；1:学分限制
                minNum: '', //分组时至少门数值
                maxNum: '', //分组时至少门数上限值
                minCredits: '', //分组时学分限制数值
                expression: '', //分组时学分限制下拉选中值
                num: 0 //模块框包含课程、模块
            },
            curCheck: [],
            schoolTerm:0,
            randomId: '', //用于事件监听和注销，确保唯一性
            tableHeight: 420,
            rowData: {}, //获取原始数据,包括课程分类和课程列表
            showCourseDialog: false,
            isElectiveForCurRow: '',
            courseTables: [],
            currentRow: {},//当前操作行,新增/编辑/删除时，保存当前操作行
            isAdd: true, //是否添加课程
            options: {
                types: [],
                directions: [],
                expressions: [
                    { label: '大于', value: '0' },
                    { label: '大于等于', value: '1' },
                    { label: '等于', value: '2' },
                    { label: '小于等于', value: '3' },
                    { label: '小于', value: '4' }
                ]
            },
            limitType: '0',
            canChangeLimitType: true,
            spanData: [],
            divideFormTitle: '',
            rule: {
                remark: [{ validator: verifyName, trigger: 'blur' }],
                minNum: [{ validator: validate, trigger: 'blur' }],
                maxNum: [{ validator: validate, trigger: 'blur' }],
                expression: [{ validator: validateChange, trigger: 'blur' }]
                // minCredits: [
                //     { validator: validate, trigger: "blur" },
                //     { validator: validateMinCredits, trigger: "blur" }
                // ]
            },
            showCourseDetailDialog: false,
            copyVisible: false,
            couseDetail: {},
            semesterListMap: [],
            semesterCodes: { '0': '暑期' },
            copyForm: {
                year: '',
                schemeId: '',
                trainingLevel: '',
                professionCateGory: '',
                courseCategory: '',
                params: {
                    labelId: '',
                    cultureId: '',
                    labelIdCopy: '',
                    cultureIdCopy: ''
                }
            },
            copyFormRules: {
                schemeId: {
                    required: true,
                    trigger: 'change',
                    message: '请选择培养方案'
                },
                year: {
                    required: true,
                    trigger: 'change',
                    message: '请选择年级'
                },
                trainingLevel: {
                    required: true,
                    trigger: 'change',
                    message: '请选择培养层次'
                },
                professionCateGory: {
                    required: true,
                    trigger: 'change',
                    message: '请选择类别'
                },
                courseCategory: {
                    required: true,
                    trigger: 'change',
                    message: '请选择课程类别'
                }
            },
            copyOptions: {
                schemes: [],
                trainingLevel: [],
                professionCateGory: [
                    { value: '1', label: '专业大类' },
                    { value: '2', label: '专业' }
                ],
                courseCategorys: []
            },
            isExceptionSetting: '',
            exception: false,
            oldTemplate:[],
            batchSetCourse: {
                show: false,
                labelId: '',
                ids: []
            }
        }
    },
    created() {
        this.randomId = 'courseDialogEvent' + new Date().getTime()
        //计算table的height值
        let elMain = document.getElementsByClassName('el-main')
        if (elMain && elMain.length > 0) {
            this.tableHeight = elMain[0].clientHeight - 100
        }
        if (this.showSCteachArrangements && elMain && elMain.length > 0) {
            this.tableHeight = elMain[0].clientHeight - 300 //这是设置强化班教学安排高度
        }
        //接受选择课程的信息
        Bus.$on(this.randomId, (state, status, courses) => {
            this.showCourseDialog = state
            this.isAdd = status
            if (courses) this.addCourseForLabel(courses)
        })
    },
    beforeDestroy() {
        Bus.$off(this.randomId)
    },
    mounted() {
        this.initOptions()
        this.init()
        this.getTemplate()
    },
    // watch: {
    //     //  sss(_n, _o){
    //     //     console.log(_n)
    //     //     console.log(_o)
    //     //     this.initSemesterListMap();
    //     // },
    //     sss: {
    //         handler(newName, oldName) {
    //              console.log(newName)
    //         console.log(oldName)
    //         },
    //         deep: true
    //     }
    // },
    methods: {
        radioChoose() {
            this.divideModuleForm.remark = ''
            this.$refs.divideModuleForm.clearValidate()
        },
        initTrainingLevelOptions() {
            findTrainingLevelByProjId(this.projId).then(res => {
                if (res.code === 200) {
                    this.copyForm.trainingLevel = ''
                    this.loadTrainingLevelOptions('trainingLevel', res.data)
                }
            })
        },
        loadTrainingLevelOptions(optionKey, dataObj) {
            this.copyOptions[optionKey] = []
            _.each(dataObj, level => {
                this.copyOptions[optionKey].push({
                    label: 'en' === this.$store.getters.language ? level.nameEN : level.nameCN,
                    value: level.code
                })
            })
        },
        handleChangScheme(id) {
            findLabelInfoByCultureId(id).then(res => {
                let { code, data } = res
                if (code !== 200) return
                this.copyOptions.courseCategorys = data
                this.copyForm.courseCategory = ''
            })
        },
        //查询培养方案列表
        handleInitScheme() {
            if (!this.copyForm.year) {
                return
            }
            const self = this
            const param = {
                id: this.$route.query.id,
                grade: this.copyForm.year,
                trainingLevel: this.copyForm.trainingLevel,
                professionCateGory: this.copyForm.professionCateGory
            }
            findCultureSchemeListByYear(param).then(res => {
                if (res && res.code === 200) {
                    self.copyOptions.schemes = []
                    self.copyForm.schemeId = ''
                    self.copyOptions.courseCategorys = []
                    self.copyForm.courseCategory = ''
                    _.forEach(res.data, item => {
                        self.copyOptions.schemes.push({
                            label: item.name,
                            value: item.id
                        })
                    })
                }
            })
        },
        deleteGroup(id) {
            this.$confirm('此操作将删除分组或模块, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteCourseLabelRelGroup({
                    id: id,
                    trainingId: this.$route.query.id
                })
                    .then(res => {
                        if (res.code == 200) {
                            this.$message({
                                type: 'success',
                                message: '删除成功'
                            })
                            this.resetPage()
                            this.init()
                        }
                    })
                    .catch(err => {
                        this.$message({
                            type: 'error',
                            message: '删除失败'
                        })
                        console.log(err)
                    })
            })
        },
        writeDirectionRel() {
            if (this.directionSelection.length == 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择方向'
                })
                return
            }
            if (this.directionSelection.length > 1) {
                this.$message({
                    type: 'warning',
                    message: '只能选择一个方向'
                })
                return
            }
            let parms = {
                directionCode: this.directionSelection[0].directionCode,
                directionName: this.directionSelection[0].directionName
            }
            parms.courseRelIds = this.curCheck
            addDirectionLableRelToCourse(parms)
                .then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        })
                        this.init()
                        this.rowData = {}
                        this.resetPage()
                        this.showDirectionDialog = false
                    }
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        message: '操作失败'
                    })
                })
        },
        selectDirection(index) {

            if (this.curCheck.length == 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择要划分方向的课程或分组'
                })
                return
            }
            let curDs = this.curCheck.map(c => {
                let lc = this.courseTables.find(ct => ct.id == c)
                if (lc) return lc.directionCode
            })
            if (curDs.every(d => d)) {
                this.$message({
                    message: '已经分方向的课程不能再分方向',
                    type: 'warning'
                })
                return
            }

            let curLabelIds = this.curCheck.map(c => {
                let lc = this.courseTables.find(ct => ct.id == c)
                if (lc) return lc.labelId
            })
            let isCurOperate = curLabelIds.every(ci => ci == this.courseTables[index].labelId)
            if (!isCurOperate) {
                    this.$message({
                        message: '请选择当前类别下的课程进行操作',
                        type: 'warning'
                    })
                    return
            }
            this.showDirectionDialog = true
        },
        directionSelectionChange(val) {
            this.directionSelection = val
        },
        // 新增课程
        add(index) {
            this.currentRow = Object.assign({}, this.courseTables[index])
            this.showCourseDialog = true
            this.isElectiveForCurRow = this.currentRow.isElective
            setTimeout(() => this.$refs.courseDialog.onSubmit())
        },
        edit(index) {
            if (!!this.currentRow && Object.keys(this.currentRow).length != 0 && !this.isAdd) {
                this.$message({
                    type: 'warning',
                    message: '请先取消/保存数据'
                })
                return
            }

            this.currentRow = Object.assign({}, this.courseTables[index])
            console.log(this.currentRow.semesters)
            //this.currentRow.semesters = this.currentRow.semesters.split(",")
            if(this.currentRow.semesters.length > 0){
                let semesters = this.currentRow.semesters
                semesters.forEach(d =>{
                    this.currentRow.semesterListMap.forEach(f =>{
                        if(d == f.key){
                            f.checked = true
                        }
                    })
                })
            }else{
                this.currentRow.semesterListMap.forEach(d =>{
                    if(d.checked){
                        d.checked = false
                    }
                })
            }
            console.log(this.currentRow)
            this.courseTables[index].edit = true
            this.isAdd = false
            this.currentRow.examMode = String(this.currentRow.examMode)
        },
        fixNum(idx) {
            if (this.currentRow['semester' + idx] < 0) {
                this.currentRow['semester' + idx] = 0
            }
        },
        fixNum1(val) {
            if (val < 0) {
                this.currentRow.computeTime = 0
            }
        },
        fixNum2(val) {
            if (val < 0) {
                this.currentRow.experimentTime = 0
            }
        },
        // saveCourse(row) {
        //     let changedSemester = [];
        //     row.semesterListMap.forEach(spec => {
        //         if (spec.checked) {
        //             changedSemester.push(spec.key);
        //         }
        //     })
        //     this.currentRow.semesters = changedSemester.join(',');
        //     let data = Object.assign({}, this.currentRow)
        //     delete data.semester
        //     data.trainingId = this.templateId
        //     data.type = this.dataType
        //     if (this.inChange) data.changeStatus = '2'
        //     EditCourse(data).then(res => {
        //         if (res.code == 200) {
        //             this.$message({
        //                 type: 'success',
        //                 message: '保存成功'
        //             })
        //             this.currentRow.semesterListMap.forEach(d =>{
        //                 if(d.checked){
        //                 d.checked = false
        //                 }
        //             })
        //             this.currentRow = {}
        //             this.init()
        //             this.curCheck.length = 0
        //         }
        //     })
        // },
        saveCourse(row) {
            let changedSemester = [];
            row.semesterListMap.forEach(spec => {
                if (spec.checked) {
                    changedSemester.push(spec.key);
                }
            })
            this.currentRow.semesters = changedSemester.join(',');
            let data = Object.assign({}, this.currentRow)
            delete data.semester
            data.trainingId = this.templateId
            data.type = this.dataType
            if (this.dataType == 1) data.changeStatus = '2'
            if (this.inChange) data.changeStatus = '2'
            let newData =  _.cloneDeep(data);
            if(data.levelType == 2){
                newData.courseGroupId = data.parentId
                newData.parentId = data.courseGroupId
            }
            console.log(newData)
            EditCourse(newData).then(res => {
                if (res.code == 200) {
                    this.$message({
                        type: 'success',
                        message: '保存成功'
                    })
                    this.currentRow.semesterListMap.forEach(d =>{
                        if(d.checked){
                        d.checked = false
                        }
                    })
                    this.currentRow = {}
                    this.init()
                    this.curCheck.length = 0
                }
            })
        },
        cancelEdit(index) {
            this.courseTables[index].edit = false
            this.courseTables[index].semesterListMap.forEach(d =>{
                    if(d.checked){
                        d.checked = false
                    }
                })
            this.isAdd = true
            this.currentRow = {}
        },
        async addCourseForLabel(courses) {
            let _this = this
            let fn = async function(params) {
                let { code, data } = await addCourse(params)
                if (code == 200) {
                    _this.init(() => {
                        if (!data || data.length == 0) {
                            _this.$message({
                                type: 'success',
                                message: '添加成功'
                            })
                        } else {
                            let str = ''
                            data.forEach((r, i) => {
                                let courseName = _this.courseTables.find(
                                    c => c.courseCode == r.courseCode
                                )
                                    ? _this.courseTables.find(c => c.courseCode == r.courseCode)
                                          .name
                                    : ''
                                if (i == data.length - 1) {
                                    str += `${courseName},`
                                } else {
                                    str += `${courseName}、`
                                }
                            })
                            let tip = `提示：${str}${data.length}门课程重复`
                            _this.$message({
                                message: tip,
                                type: 'warning'
                            })
                        }
                    })
                }
            }
            let dataAdd = _this.getAddData(courses)
            if (dataAdd.directionCode) {
                //方向下重复校验
                let { code, data } = await checkRepeatedMultiDirection({
                    trainingID: dataAdd.trainingId,
                    directionCode: dataAdd.directionCode,

                    courseCodes: dataAdd.courseCodes + ''
                })
                if (code == 200 && data) {
                    this.$confirm('您添加的课程与已有课程重复，是否确认添加？')
                        .then(_ => {
                            fn(dataAdd)
                        })
                        .catch(_ => {
                            console.log(_)
                        })
                } else {
                    fn(dataAdd)
                }
            } else {
                fn(dataAdd)
            }
        },
        // getAddData(courses) {
        //     courses.courseCodes = courses.item.map(item => {
        //         return item.code
        //     })
        //     courses.trainingId = this.templateId
        //     courses.type = this.dataType
        //     courses.labelId = this.currentRow.labelId
        //     courses.courseList = []
        //     courses.item.forEach(row => {
        //         courses.courseList.push({
        //             courseCode: row.code,
        //             examMode: row.examMode,
        //             compulsory: row.compulsory,
        //             weeksType: row.weeksType,
        //             trainingId: this.templateId,
        //             type: this.dataType,
        //             labelId: this.currentRow.labelId
        //         })
        //     })
        //     if (
        //         this.currentRow.hasOwnProperty('directionCode') &&
        //         this.currentRow.directionCode != ''
        //     )
        //         courses.directionName = this.currentRow.categoryName
        //     courses.directionCode = this.currentRow.directionCode
        //     if (this.inChange) courses.changeStatus = '1'
        //     delete courses.item
        //     return courses
        // },
        getAddData(courses) {
            courses.courseCodes = courses.item.map(item => {
                return item.code
            })
            courses.trainingId = this.templateId
            courses.type = this.dataType
            courses.labelId = this.currentRow.labelId
            courses.courseList = []
            if(this.dataType == 1){
                courses.item.forEach(row => {
                    courses.courseList.push({
                        changeStatus: '1',
                        courseCode: row.code,
                        examMode: row.examMode,
                        compulsory: row.compulsory,
                        weeksType: row.weeksType,
                        trainingId: this.templateId,
                        type: this.dataType,
                        labelId: this.currentRow.labelId,
                        semesters: (row.semesters || []).join()
                    })
                })
            }else{
                courses.item.forEach(row => {
                    courses.courseList.push({
                        courseCode: row.code,
                        examMode: row.examMode,
                        compulsory: row.compulsory,
                        weeksType: row.weeksType,
                        trainingId: this.templateId,
                        type: this.dataType,
                        labelId: this.currentRow.labelId,
                        semesters: (row.semesters || []).join()
                    })
                })
            } 
            if (
                this.currentRow.hasOwnProperty('directionCode') &&
                this.currentRow.directionCode != ''
            )
                courses.directionName = this.currentRow.categoryName
            courses.directionCode = this.currentRow.directionCode
            if (this.inChange) courses.changeStatus = '1'
            delete courses.item
            return courses
        },
        /**
         * level 1:勾选课程复选框；2:勾选模块复选框;3:勾选分组复选框
         */
        pickCourse(id, index, level) {
            if (event.target.checked) {
                if (this.canCheck(id, index, level, this)) {
                    this.divideModuleForm.level = level

                    switch (level) {
                        case 1:
                            this.curCheck.push(id)
                            break
                        case 2:
                            var curGroupId = this.courseTables[index].courseGroupId
                            this.courseTables
                                .filter(c => c.courseGroupId == curGroupId)
                                .forEach(cg => this.curCheck.push(cg.id))
                            break
                        case 3:
                            var curPid = this.courseTables[index].parentId
                            this.courseTables
                                .filter(c => c.parentId == curPid)
                                .forEach(cg => this.curCheck.push(cg.id))
                            break
                    }
                }
            } else {
                if (this.curCheck.indexOf(id) >= 0) {
                    switch (level) {
                        case 1:
                            this.curCheck.splice(this.curCheck.indexOf(id), 1)
                            this.courseTables[index].isCourseCheck = false
                            break
                        case 2:
                            var curGroupId = this.courseTables[index].courseGroupId
                            var gids = []
                            this.courseTables.forEach(c => {
                                if (c.courseGroupId == curGroupId) {
                                    gids.push(c.id)
                                }
                            })
                            var copyCurCheck = _.cloneDeep(this.curCheck)
                            this.curCheck = []
                            copyCurCheck.forEach(c => {
                                if (gids.findIndex(g => g == c) == -1) {
                                    this.curCheck.push(c)
                                }
                            })
                            this.courseTables.forEach(c => {
                                if (copyCurCheck.findIndex(d => c.id == d.id) > -1) {
                                    c.isCourseCheck = false
                                    c.isModuleCheck = false
                                }
                            })
                            break
                        case 3:
                            var curPid = this.courseTables[index].parentId
                            var copyCurCheck = _.cloneDeep(this.curCheck)

                            var pids = []
                            this.courseTables.forEach(c => {
                                if (c.parentId == curPid) {
                                    pids.push(c.id)
                                }
                            })
                            this.curCheck = []
                            copyCurCheck.forEach(c => {
                                if (pids.findIndex(p => p == c) == -1) {
                                    this.curCheck.push(c)
                                }
                            })
                            this.courseTables.forEach(c => {
                                if (copyCurCheck.findIndex(d => c.id == d.id) > -1) {
                                    c.isCourseCheck = false
                                    c.isModuleCheck = false
                                }
                            })
                            break
                    }
                }
            }
            if (this.curCheck.length == 0) {
                this.divideModuleForm.level = ''
            }
        },
        showMessage(context, msg) {
            context.$message({
                message: '不能跨方向、课程、模块、分组操作',
                type: 'warning'
            })
            return
        },
        cancelCheck(context, level, id, index) {
            let _this = context
            switch (level) {
                case 1:
                    _this.courseTables[index].isCourseCheck = false
                    break
                case 2:
                    var curGroupId = _this.courseTables[index].courseGroupId

                    _this.courseTables.forEach(c => {
                        if (c.courseGroupId == curGroupId) {
                            c.isModuleCheck = false
                        }
                    })
                    break
                case 3:
                    var curPid = _this.courseTables[index].parentId
                    _this.courseTables.forEach(c => {
                        if (c.parentId == curPid) {
                            c.isGroupCheck = false
                        }
                    })
                    break
            }
        },
        canCheck(id, index, level, context) {
            if (!this.curCheck.length) return true
            let checkedObj = this.courseTables.find(
                c => c.id == this.curCheck[this.curCheck.length - 1]
            )
            let curCheckObj = this.courseTables[index]
            if (curCheckObj.directionCode !== '') {
                if (
                    checkedObj.labelId == curCheckObj.labelId &&
                    checkedObj.directionCode == curCheckObj.directionCode
                ) {
                    return true
                } else {
                    this.cancelCheck(context, level, id, index)
                    this.showMessage(context)
                }
            } else {
                switch (level) {
                    case 1:
                        if (
                            checkedObj.labelId == curCheckObj.labelId &&
                            checkedObj.levelType == ''
                        ) {
                            return true
                        } else {
                            this.cancelCheck(context, level, id, index)
                            this.showMessage(context)
                        }
                        break
                    case 2:
                        if (
                            checkedObj.labelId == curCheckObj.labelId &&
                            +checkedObj.levelType == 3
                        ) {
                            return true
                        } else {
                            this.cancelCheck(context, level, id, index)
                            this.showMessage(context)
                        }
                        break
                    case 3:
                        if (
                            checkedObj.labelId == curCheckObj.labelId &&
                            +checkedObj.levelType == 2
                        ) {
                            return true
                        } else {
                            this.cancelCheck(context, level, id, index)
                            this.showMessage(context)
                        }
                        break
                }
            }
        },
        // 复制之前
        copyBefore(row) {
            this.copyForm.params.labelId = +row.labelId
            this.copyForm.params.cultureId = this.$route.query.id
            this.copyVisible = true
            this.initTrainingLevelOptions() //获取培养层次列表数据
        },
        handleSubmitCopy() {
            this.$refs.copyForm.validate(valid => {
                if (valid) {
                    this.copyForm.params.labelIdCopy = this.copyForm.courseCategory
                    this.copyForm.params.cultureIdCopy = this.copyForm.schemeId
                    copyLabelCourseRelation(this.copyForm.params).then(res => {
                        if (res && res.code === 200) {
                            this.copyVisible = false
                            this.$message({
                                message: res.data ? `${res.msg},${res.data}` : res.msg,
                                type: res.data ? 'warning' : 'success'
                            })
                            this.init()
                        }
                    })
                }
            })
        },
        // 分组分模块，type 1为模块，2为分组
        showDivideModule(index, type) {
            if (this.curCheck.length <= 1) {
                this.$message({
                    message: '分组或者分模块的课程数必须大于1门',
                    type: 'warning'
                })
                return
            }
            let curLabelIds = this.curCheck.map(c => {
                let lc = this.courseTables.find(ct => ct.id == c)
                if (lc) return lc.labelId
            })
            let isCurOperate = curLabelIds.every(ci => ci == this.courseTables[index].labelId)
            if (!isCurOperate) {
                    this.$message({
                        message: '请选择当前类别下的课程进行操作',
                        type: 'warning'
                    })
                    return
            }
            if (this.divideModuleForm.level == 2 && type == 1) {
                this.$message({
                    message: '模块不能再次划分模块',
                    type: 'warning'
                })
                return
            }
            if (this.divideModuleForm.level == 3 && type == 2) {
                this.$message({
                    message: '分组不能再次划分分组',
                    type: 'warning'
                })
                return
            }
            if (this.divideModuleForm.level == 3 && type == 1) {
                this.$message({
                    message: '分组不能再次划分模块',
                    type: 'warning'
                })
                return
            }
            this.divideModuleForm.title = type == 1 ? '划分模块' : '课程分组'
            this.divideModuleForm.operate = type
            this.currentRow = this.courseTables[index]
            if (this.currentRow.limitType) {
                this.canChangeLimitType = false
                this.limitType = this.currentRow.limitType
            }
            this.setCurrentCourses()
            // this.setDivideForm(index, type);
            this.divideModuleForm.num = this.getCheckedNum(
                this.curCheck,
                this.divideModuleForm.level
            )
            this.divideModuleForm.maxNum = this.getCheckedNum(
                this.curCheck,
                this.divideModuleForm.level
            )
            this.showDivideModuleDialog = true
        },
        // 批量删除
        async bathDeleta(index) {
            if (this.curCheck.length == 0) {
                this.$message({
                    message: '请选择要删除的课程',
                    type: 'warning'
                })
                return
            }
            let curLabelIds = this.curCheck.map(c => {
                let lc = this.courseTables.find(ct => ct.id == c)
                if (lc) return lc.labelId
            })
            let isCurOperate = curLabelIds.every(ci => ci == this.courseTables[index].labelId)
            if (!isCurOperate) {
                    this.$message({
                        message: '请选择当前类别下的课程进行操作',
                        type: 'warning'
                    })
                    return
            }
            if (this.divideModuleForm.level == 2) {
                this.$message({
                    message: '请先删除模块再进行操作',
                    type: 'warning'
                })
                return
            }
            if (this.divideModuleForm.level == 3) {
                this.$message({
                    message: '请先删除分组再进行操作',
                    type: 'warning'
                })
                return
            }
            await this.$confirm(
                "确定要删除吗?",
                "提示",
                {
                    confirmButtonText: this.$t("common.btn.confirm"),
                    cancelButtonText: this.$t("common.btn.cancel"),
                    type: "warning"
                }
            );
            let ids = _.cloneDeep(this.curCheck)
            ids = ids.toString();
            batchDelCourseLabeRalation(this.templateId,ids).then(res => {
                if (res.code == 200) {
                    this.$message({
                        message: '操作成功',
                        type: 'success'
                    })
                    this.init()
                    this.resetPage()
                }
            })
        },
        bathEdit(index) {
            if (this.curCheck.length == 0) {
                this.$message({
                    message: '请选择要编辑的课程',
                    type: 'warning'
                })
                return;
            }
            let curLabelIds = this.curCheck.map(cur => {
                let course = this.courseTables.find(item => item.id == cur)
                if (course) return course.labelId
            })
            let labelId = this.courseTables[index].labelId;
            let isCurOperate = curLabelIds.every(i => i == labelId)
            if (!isCurOperate) {
                    this.$message({
                        message: '请选择当前类别下的课程进行操作',
                        type: 'warning'
                    })
                    return;
            }
            Object.assign(this.batchSetCourse, {
                labelId,
                ids: [...this.curCheck],
                show: true
            })
        },
        async batchSetCourseConfirm(data) {
            if (!data) {
                this.batchSetCourse.show = false;
                return;
            }
            let {ids, labelId} = this.batchSetCourse;
            let param = {
                labelId,
                ids,
                trainingId: this.templateId,
                type: this.dataType,
                ...data
            };
            let {code} = await batchUpdateCourseLabelRelation(param);
            if (code == 200) {
                this.batchSetCourse.show = false;
                this.$message({
                    message: '操作成功',
                    type: 'success'
                });
                this.init()
                this.resetPage()
            }
        },
        setCurrentCourses() {
            if (this.divideModuleForm.level == 2) {
                //分组
                let count = 0
                this.currentCourses = []
                this.curCheck.forEach(e => {
                    let span = {
                        rowIndex: count
                    }
                    this.courseTables.forEach(i => {
                        if (i.id == e) {
                            this.currentCourses.push(i)
                            count++
                        }
                    })
                    span.rowspan = count - span.rowIndex
                    this.spanData.push(span)
                })
            } //分模块
            else
                this.currentCourses = this.curCheck.map(e => {
                    return this.courseTables.find(i => {
                        return i.id == e
                    })
                })
        },
        setDivideForm(index, type) {
            this.divideModuleForm.operate = type
            this.currentRow = this.courseTables[index]
            if (this.currentRow.limitType) {
                this.canChangeLimitType = false
                this.limitType = this.currentRow.limitType
            }
            // let level = this.divideModuleForm.level;
            //type 1为模块，2为分组
            // level 1为模块分组，2为课程分组，3课程分模块
            //课程分模块
            // if (level == 1 && type == 1) this.divideModuleForm.level = 3;
            //课程分组
            // else if (level == 1 && type == 2) this.divideModuleForm.level = 2;
            //模块分组
            // else if (level == 2 && type == 2) this.divideModuleForm.level = 1;
        },
        writeDivideRelation() {
            this.$refs['divideModuleForm'].validate(valid => {
                if (valid) {
                    const level = this.divideModuleForm.level
                    const type = this.divideModuleForm.operate
                    //课程分模块
                    if (level == 1 && type == 1) this.divideModuleForm.level = 3
                    //课程分组
                    else if (level == 1 && type == 2) this.divideModuleForm.level = 2
                    //模块分组
                    else if (level == 2 && type == 2) this.divideModuleForm.level = 1
                    this.divideModuleForm.courseIdList = this.curCheck
                    this.divideModuleForm.type = this.limitType
                    addCourseLabelRelGroup(this.divideModuleForm).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            })
                            this.init()
                            this.resetPage()
                            this.currentRow = {}
                        }
                    })
                } else return false
            })
        },
        resetPage() {
            this.curCheck = []
            this.currentCourses = []
            this.limitType = '0'
            this.canChangeLimitType = true
            for (let key in this.divideModuleForm) {
                if (key == 'courseIdList') {
                    this.divideModuleForm[key] = []
                } else if (key == 'type') {
                    this.divideModuleForm[key] = '0'
                } else if (key == 'num') {
                    this.divideModuleForm[key] = 0
                } else {
                    this.divideModuleForm[key] = ''
                }
            }
            this.showDivideModuleDialog = false
        },
        initOptions() {
            let data = {
                lang: this.$store.getters.language || 'cn',
                type: 'allChild',
                keys: ['X_KSLX']
            }
            queryDic(data)
                .then(res => {
                    if (res.code == 200) {
                        this.loadOptions('types', res.data.X_KSLX)
                    }
                })
                .catch(err => {
                    this.$message({
                        type: 'error',
                        message: '查询失败'
                    })
                })

                if(this.dataType == 2){
                    this.initSemesterListMap();
                }else{
                    getCultureTemplateById(this.templateId).then(response => {
                        if (response.code === 200) {
                            this.schoolTerm = response.data.schoolTerm;
                            let schoolTerm = 0
                            schoolTerm = response.data.schoolTerm;
                            let list = [];
                            let labelArr = ['一',  '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十'];
                            for(let i = 0 ; i< schoolTerm; i++) {
                                list.push({
                                    label: labelArr[i],
                                    checked: false,
                                    key: i + 1
                                })
                            }
                            this.semesterListMap = list;
                        }
                    });
                }
        },
        initSemesterListMap() {

            if(this.schoolTerms){
                let schoolTerm = 0
                schoolTerm = this.schoolTerms
                this.schoolTerm = this.schoolTerms
                let list = [];
                let labelArr = ['一',  '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十'];
                for(let i = 0 ; i< schoolTerm; i++) {
                    list.push({
                        label: labelArr[i],
                        checked: false,
                        key: i + 1
                    })
                }
                    this.semesterListMap = list;
            }else{
                getSchemeSchoolTerm(this.templateId).then(res => {
                    if (res.code == 200) {
                        let schoolTerm = 0
                        schoolTerm = res.data.schoolTerm 
                        this.schoolTerm = res.data.schoolTerm 
                        let list = [];
                        let labelArr = ['一',  '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二', '十三', '十四', '十五', '十六', '十七', '十八', '十九', '二十'];
                        for(let i = 0 ; i< schoolTerm; i++) {
                            list.push({
                                label: labelArr[i],
                                checked: false,
                                key: i + 1
                            })
                        }
                        this.semesterListMap = list;
                    }
                    
                })
            }
        },
        cellStyleMethod({row, column, rowIndex, columnIndex}){
            if (column.label && row.semesterListMap) {
                let currentItem = _.find(row.semesterListMap, item => {
                    return item.label === column.label;
                })
                if (currentItem) {
                   if (row.edit) {
                        if (currentItem.checked) {
                            return 'background: #3A7FE8; cursor: pointer';
                        }
                        return "cursor: pointer";
                   }
                }
           }
        },
        cellCLickMethod(row, column) {
            console.log(row)
            let flag = true
            if (!row.edit) return;
            if (row.edit && !row.exception && row.semesters.length > 0) {
                console.log(this.oldTemplate)
                this.oldTemplate.forEach(d =>{
                    if(d.courseCode == row.courseCode){
                        console.log(d.semesters)
                        flag = d.semesters == ""
                        console.log(flag)
                    }
                })
                if(!flag){
                    return
                }
            };
            if (column.label && row.semesterListMap) {
                let currentItem = _.find(row.semesterListMap, item => {
                    return item.label === column.label;
                })
                if (currentItem) {
                    currentItem.checked =  !currentItem.checked;
                }
           }
        },
        // cellCLickMethod(row, column) {
        //     if (!row.edit) return;
        //     if (column.label && row.semesterListMap) {
        //         let currentItem = _.find(row.semesterListMap, item => {
        //             return item.label === column.label;
        //         })
        //         if (currentItem) {
        //             currentItem.checked =  !currentItem.checked;
        //         }
        //    }
        // },
        // checkSemester(val,semester){
        //     console.log(val)
        //     console.log(semester)
        //     if(semester){
        //        val.selected = true; 
        //     }else{
        //         val.selected = false;
        //     }
        //     this.$forceUpdate()
        //     console.log(this.currentRow)
        // },
        loadOptions(optionKey, dataObj) {
            for (let key in dataObj) {
                if (key == 1 || key == 2) {
                    this.options[optionKey].push({
                        value: key,
                        label: dataObj[key]
                    })
                }
            }
        },
        // remove(index) {
        //     this.isExceptionSetting = ''
        //     let row = this.courseTables[index]
        //     if (row.courseGroupId != '' || row.parentId != '') {
        //         this.$message({
        //             message: '已经分组或者分模块的课程不能删除',
        //             type: 'warning'
        //         })
        //         return
        //     }
        //     let de = function() {
        //         this.$confirm('是否删除该课程?', '提示', {
        //             confirmButtonText: '确定',
        //             cancelButtonText: '取消',
        //             type: 'warning'
        //         }).then(() => {
        //             deleteCourse(row.id).then(res => {
        //                 if (res.code == 200) {
        //                     this.$message({
        //                         type: 'success',
        //                         message: '删除成功!'
        //                     })
        //                     this.init()
        //                 } else if (res.code == 300) {
        //                     this.$message({
        //                         type: 'warning',
        //                         message: '不能删除该条记录!'
        //                     })
        //                 }
        //             })
        //         })
        //     }
        //     // row.status 1:可以直接删除；2：在模板里面的课程
        //     // if(status ==2) 判断当前课程是否在例外设置里
        //     if (row.status == 2) {
        //         findExceptions(this.cultureSchemeForcourse).then(res => {
        //             if (res.code == 200) {
        //                 this.isExceptionSetting = res.data.total_
        //                 if (this.isExceptionSetting == 0) {
        //                     this.$message({
        //                         message: '从培养模板继承过来的课程不能删除',
        //                         type: 'warning'
        //                     })
        //                 }else{
        //                     de.call(this)
        //                 }
        //             }
        //         })
        //     } else {
        //         de.call(this)
        //     }
        // },
        remove(index) {
            this.isExceptionSetting = ''
            let row = this.courseTables[index]
            if (row.courseGroupId != '' || row.parentId != '') {
                this.$message({
                    message: '已经分组或者分模块的课程不能删除',
                    type: 'warning'
                })
                return
            }
            let de = function() {
                this.$confirm('是否删除该课程?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteCourse(row.id).then(res => {
                        if (res.code == 200) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            })
                            this.init()
                        } else if (res.code == 300) {
                            this.$message({
                                type: 'warning',
                                message: '不能删除该条记录!'
                            })
                        }
                    })
                })
            }
            // row.status 1:可以直接删除；2：在模板里面的课程
            // if(status ==2) 判断当前课程是否在例外设置里
            if (row.status == 2) {
                findExceptions(this.cultureSchemeForcourse).then(res => {
                    if (res.code == 200) {
                        this.isExceptionSetting = res.data.total_
                        if (this.isExceptionSetting == 0) {
                            this.$message({
                                message: '从培养模板继承过来的课程不能删除',
                                type: 'warning'
                            })
                        }else{
                            de.call(this)
                        }
                    }
                })
            } else {
                de.call(this)
            }
        },
        getTemplate() {
            let templateIdOrigin = this.$route.query.templateId
            if(this.dataType == 2 && templateIdOrigin){             
                courseLabelRelation(templateIdOrigin, '1')
                .then(res => {
                    if (res.code == 200) {
                        this.oldTemplate = res.data.courseLabelRelationList
                    }
                })
                .catch(e => {
                    console.log(e)
                })
            }
        },
        //获取后台相关课程数据
        init(cb) {
            this.rowData = {}
            this.rowData.courseCategories = []
            courseLabelRelation(this.templateId, this.dataType)
                .then(res => {
                    if (res.code == 200) {
                        cb && cb()
                        //保存类别数据
                        this.rowData.courseCategories =
                            res.data &&
                            res.data.coursesLabelList.map(c => {
                                //课程类型：公共学位课，必修课。。。
                                return {
                                    id: c.labelId,
                                    name: c.label,
                                    labelId: c.parentId,
                                    isElective: c.isElective,
                                    limitType: c.limitType,
                                    orderNo: c.orderNo,
                                    isPublicElective: c.isPublicElective, //是否公共课
                                }
                            })
                        // 保存方向数据
                        this.rowData.directionList =
                            res.data && res.data.directionList.filter(e => e.directionName)
                        if(res.data.isExceptionSetting == 1){
                            this.exception = res.data.isExceptionSetting == 1?true:false
                        }
                        this.setCourseData(res.data.courseLabelRelationList)
                        this.appendCategory()
                        console.log(this.courseTables)
                    }
                })
                .catch(e => {
                    console.log(e)
                    this.$message({
                        type: 'error',
                        message: '查询课程表单失败,请稍后再试'
                    })
                })
        },
        setCourseData(list) {
            let _this = this
            _this.rowData.courses = []
            let fn = function(value) {
                let item = _this.options.types.find(t => t.value == value)
                return item ? item.label : ''
            }
            if(this.exception){
                if(this.exception){
                    list.forEach(ele => {
                        ele.exception = true
                        ele.edit = false
                        ele.isCourseRow = true
                        ele.isGroupCheck = false
                        ele.isCourseCheck = false
                        ele.isModuleCheck = false
                        ele.studyScore = ele.credits
                        ele.semesters = ele.semesters?ele.semesters.split(","):[]
                        ele.semesterListMap = _.cloneDeep(this.semesterListMap);
                        ele.parentId = ele.levelType == 2 ? ele.courseGroupId : ele.parentId
                        ele.courseGroupId = ele.levelType == 2 ? '' : ele.courseGroupId
                        ele.examModeName = fn(ele.examMode)
                        _this.rowData.courses.push(Object.assign({}, ele))
                    })
                }
            }else{
                list.forEach(ele => {
                    if( ele.status == 2){
                        ele.exception = false
                        
                    }else{
                        ele.exception = true
                    }
                    ele.edit = false
                    ele.isCourseRow = true
                    ele.isGroupCheck = false
                    ele.isCourseCheck = false
                    ele.isModuleCheck = false
                    ele.studyScore = ele.credits
                    ele.semesters = ele.semesters?ele.semesters.split(","):[]
                    ele.semesterListMap = _.cloneDeep(this.semesterListMap);
                    ele.parentId = ele.levelType == 2 ? ele.courseGroupId : ele.parentId
                    ele.courseGroupId = ele.levelType == 2 ? '' : ele.courseGroupId
                    ele.examModeName = fn(ele.examMode)
                    _this.rowData.courses.push(Object.assign({}, ele))
                })
                
            }
            // list.forEach(ele => {
            //     // if (compareList && compareList.length) {
            //     //     for (let i = 0; i < compareList.length; i++) {
            //     //         let row = compareList[i];
            //     //         if (ele.id === row.id && row.isCourseCheck) {
            //     //             ele.isCourseCheck = true;
            //     //             break;
            //     //         }
            //     //     }
            //     // }
                
            //     ele.exception = ele.status == 2 ? false:true
            //     ele.edit = false
            //     ele.isCourseRow = true
            //     ele.isGroupCheck = false
            //     ele.isCourseCheck = false
            //     ele.isModuleCheck = false
            //     ele.studyScore = ele.credits
            //     ele.semesters = ele.semesters.split(",")
            //     ele.semesterListMap = _.cloneDeep(this.semesterListMap);
            //     ele.parentId = ele.levelType == 2 ? ele.courseGroupId : ele.parentId
            //     ele.courseGroupId = ele.levelType == 2 ? '' : ele.courseGroupId
            //     ele.examModeName = fn(ele.examMode)
            //     _this.rowData.courses.push(Object.assign({}, ele))
            // })
        },
        appendDirection(key) {
            let _this = this
            let list = []
            let directionList = _.cloneDeep(_this.rowData.directionList)
            directionList.forEach((d, i) => {
                let item = {}
                item.sequence = ''
                item.categoryName = d.directionName
                item.labelId = key
                item.directionCode = d.directionCode
                item.isCategoryRow = true
                item.showAddCourseBtn = true
                item.isDirection = true
                item.hasCourse = this.rowData.courses.some(c => c.directionCode == d.directionCode)
                list.push(item)
                let childCourseList = this.rowData.courses.filter(
                    c => c.labelId == key && c.directionCode == d.directionCode
                )
                list = [...list, ...childCourseList]
            })
            return list
        },
        appendCategory() {
            //递归树
            let ft = function(result, list) {
                result.forEach(l => {
                    let childList = list.filter(r => r.labelId == l.id)
                    while (childList.length > 0) {
                        l.children = childList
                        return ft.call(this, childList, list)
                    }
                })
                return result
            }
            //数组扁平化
            let fn = function(arr) {
                let result = []
                let line = function(arr) {
                    arr.forEach(item => {
                        result.push(item)
                        while (item.children && item.children.length > 0) return line(item.children)
                    })
                }
                line(arr)
                return result
            }
            //追加课程数据
            let addFn = function(arr) {
                let result = []
                arr.forEach(item => {
                    result.push(item)
                    if (item.hasCourse) {
                        if (this.rowData.directionList.length > 0) {
                            result = result.concat(
                                this.appendCourse(item.id),
                                this.appendDirection(item.id)
                            )
                        } else {
                            result = result.concat(this.appendCourse(item.id))
                        }
                    }
                })
                return result
            }
            let result = this.rowData.courseCategories.filter(
                l => l.labelId == '0' || l.labelId == ''
            )
            result.forEach((item, index) => {
                item.sequence = `${this.$utils.numberToChinese(index + 1)}、`
            })
            let list = fn(ft.call(this, result, this.rowData.courseCategories)).map(c => {
                return Object.assign(c, {
                    categoryName: c.name,
                    labelId: c.id,
                    isCategoryRow: true,
                    isElective: c.isElective, //是否公共选修
                    showAddCourseBtn: !c.children,
                    isPublicElective: c.isPublicElective, //是否公共课
                    hasCourse: this.hasCourse(c.id)
                })
            })
            console.log(list)
            this.courseTables = addFn.call(this, list)
            // this.courseTables.forEach(d =>{
            //     this.semesterSorts.forEach((f,index) =>{ 
            //         if(d['semester' + (index+1)] == f.value){
            //             d['semester' + (index+1)] = d.weekHour
            //             f.selected = true
            //         }
            //     })
            // })
        },
        /**是否有课程 */
        hasCourse(labelId) {
            return this.rowData.courses.some(i => {
                return i.labelId == labelId
            })
        },
        //添加课程项
        appendCourse(courseCategoryId, isElective = '') {
            let childCourseList = this.rowData.courses.filter(
                c => c.labelId == courseCategoryId && !c.directionCode
            )
            childCourseList.forEach(item => {
                ;(item.labelId = courseCategoryId), (item.isElective = isElective)
            })
            return childCourseList
        },
        //合并模块模态框内模块合并
        spanMethodForModule({ row, column, rowIndex, columnIndex }) {
            if (this.divideModuleForm.level == 2) {
                if (columnIndex == 0) {
                    let d = this.spanData.filter(ele => {
                        return ele.rowIndex == rowIndex
                    })
                    if (d.length > 0) {
                        return {
                            rowspan: d[0].rowspan,
                            colspan: 1
                        }
                    } else {
                        if (this.divideModuleForm.level == 1)
                            return {
                                rowspan: 0,
                                colspan: 1
                            }
                        else
                            return {
                                rowspan: 1,
                                colspan: 1
                            }
                    }
                }
            }
        },
        //合并列表单元格
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (row.isCategoryRow) {
                if (columnIndex == 0) {
                    return {
                        rowspan: 1,
                        colspan: 2
                    }
                } else if (columnIndex == 2) {
                    return {
                        rowspan: 1,
                        colspan: this.isRead ? 9 + this.schoolTerm : 10 + this.schoolTerm
                    }
                }
                else if (columnIndex !== 0 && columnIndex != 2) {
                    return {
                        rowspan: 0,
                        colspan: 0
                    }
                }
            }
            if (columnIndex == 2 || columnIndex == 3) {
                let spanData = columnIndex == 2 ? this.getspanData(1) : this.getspanData(2)
                let d = spanData.filter(ele => {
                    return ele.rowIndex == rowIndex
                })
                if (d.length > 0) {
                    return {
                        rowspan: d[0].rowspan,
                        colspan: 1
                    }
                }
            }
        },
        getspanData(type) {
            //type==1 分模块, type ==2 分组
            let labelId = type == 1 ? 'courseGroupId' : 'parentId'
            let curPid = -1,
                curRowIndex,
                rowspan,
                spanData = []
            this.courseTables.forEach((ele, i, arr) => {
                if (ele.isCourseRow && ele[labelId]) {
                    if (ele[labelId] != curPid) {
                        if (curPid != -1) {
                            spanData.push({
                                rowIndex: curRowIndex,
                                rowspan: rowspan
                            })
                        }
                        curPid = ele[labelId]
                        curRowIndex = i
                        rowspan = 1
                    } else {
                        spanData.push({
                            rowIndex: i,
                            rowspan: 0
                        })
                        rowspan++
                    }
                }
            })
            spanData.push({
                rowIndex: curRowIndex,
                rowspan: rowspan
            })
            return spanData
        },
        showCourseDetail(c) {
            this.couseDetail = {
                ...c,
                week_hour: c.weekHour
            }
            this.showCourseDetailDialog = true
        },
        handlerCloseCourseDialog() {
            this.showCourseDetailDialog = false
        },
        getCheckedNum(ids, level) {
            let num = 0
            switch (level) {
                case 1: //课程
                    num = ids.length
                    return num
                    break
                case 2: //模块
                    let curCheckList = []
                    this.courseTables.forEach(c => {
                        if (ids.find(i => i == c.id)) {
                            curCheckList.push(c.courseGroupId)
                        }
                    })
                    num = Array.from(new Set(curCheckList)).length
                    return num
                    break
            }
        },
        resetObj(params) {
            for (let key in params) {
                if (Object.prototype.toString.call(params[key]) == '[object Object]') {
                    this.resetObj(params[key])
                } else {
                    params[key] = ''
                }
            }
        },
        autoName() {
            let name,
                obj = this.divideModuleForm
            switch (this.limitType) {
                case '0':
                    name = `${+obj.level == 1 ? '课程' : '模块'}${obj.num}选${obj.minNum}`
                    this.divideModuleForm.remark = name
                    break
                case '1':
                    if (!obj.expression) return
                    name = `学分${
                        this.options.expressions.find(e => e.value == obj.expression).label
                    }${obj.minCredits}`
                    this.divideModuleForm.remark = name
                    break
            }
        }
    },
    watch: {
        showDivideModuleDialog: function(n, o) {
            if (!n) {
                this.$refs.divideModuleForm.resetFields()
            }
        },
        copyVisible: function(n, o) {
            if (!n) {
                this.$refs.copyForm.resetFields()
                this.resetObj(this.copyForm)
                this.copyOptions.schemes = []
                this.copyOptions.trainingLevel = []
                this.copyOptions.courseCategorys = []
            }
        },
        schoolTerms: function(n, o) {
            console.log(n)
            console.log(o)
        },
        templateId: function(n, o) {
            if(n){
                this.courseTables = []
                this.initOptions()
                this.init()
            }
        },
    }
}
</script>
<style  scoped>
.el-col >>> .el-radio {
    margin-right: 0px;
}
.el-col >>> .el-radio__label {
    padding-left: 0px;
}
.el-checkbox {
    margin-right: 0px;
}
.el-table{
  overflow: auto;
}
.el-table__body-wrapper{
    overflow:visible;
}
.el-table::after{
    position: relative;
}
</style>